<?php
class base {
    public $ctx;
    public $db;
    private $table;
    private $primary;
    function __construct($table,$primary) {
        $this->ctx = $GLOBALS['context'];
        $this->db = $this->ctx->db;
        $this->table = $table;
        $this->primary = $primary;
    }
    function get_upload_img_name ( array & $request, array & $response, array & $app ) {
        $upload_name = $request['upload_name'];
        $img_dir = $request['img_dir'];
        $file_name = $request['file_name'];
        $is_mark  = isset($request['is_mark'])?$request['is_mark']:0;//是否启用水印
        $res = load_model('image')->upload_image ( $_FILES[$upload_name], $img_dir, $file_name ,$is_mark, 'system_images', $upload_name);
        if($is_mark) {

        }else {

        }
        return $res ? $res : array();
    }

    function show_img(array & $request) {
        if(isset($request['id']) && $request['id']) {
            $username = $this->ctx->get_session('doctor_user_name', true);
            if($username) {
                $date = $this->ctx->db->create_mapper('DoctorProofImage')->where(array('ImageId'=>$request['id']))->find_by();
                if($date && $date['Content']) {
                    header('Content-type:image/JPEG',true);
                    echo $date['Content'];
                }
            }
        }
        exit;
    }
    function get_item($param = array()) {
        $obj = $this->db->create_mapper($this->table);
        if($param) {
            $obj->where($param);
        }
        $result = $obj->find_by();
        return $result ? $result : array();
    }
    function get_items($param = array()) {
        $obj = $this->db->create_mapper($this->table);
        if($param) {
            $obj->where($param);
        }
        $result = $obj->find_all_by();
        return $result ? $result : array();
    }
    function update_item($params = array(), $where =array(), $field =array()) {
        try{
            if($params && $where) {
                $res = $this->db->create_mapper($this->table)->update($params, $where, $field);
            }else {
                $res = false;
            }
        }catch (Exception $e) {
            $this->ctx->log_error('sql_error:'.$e->getMessage());
            $res = false;
        }
        return $res ?  true : false;
    }
    function insert_item($params = array()) {
        if($params) {
            try {
                $res = $this->db->create_mapper($this->table)->insert($params, true);
            }catch(Exception $e) {
                $this->ctx->log_error('sql_error:'.$e->getMessage());
                $res = false;
            }
        }else {
            $res = false;
        }
        return $res ?  true : false;
    }
    function delete_item($primary) {
        if($primary) {
            try {
                $res = $this->db->create_mapper($this->table)->delete(array($this->primary=> $primary));
            }catch(Exception $e) {
                $this->ctx->log_error('sql_error:'.$e->getMessage());
                $res = false;
            }
        }else {
            $res = false;
        }
        return $res ?  true : false;
    }
}
